package com.ruoyi.weixin.shop.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.weixin.shop.domain.WxShare;
import com.ruoyi.weixin.shop.domain.vo.WxShareAddBo;
import com.ruoyi.weixin.shop.domain.vo.WxShareEditBo;
import com.ruoyi.weixin.shop.domain.vo.WxShareQueryBo;
import com.ruoyi.weixin.shop.domain.vo.WxShareVo;
import com.ruoyi.weixin.shop.mapper.WxShareMapper;
import com.ruoyi.weixin.shop.service.IWxShareService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 微信商城分享Service业务层处理
 *
 * @author ruoyi
 * @date 2021-07-12
 */
@Service
public class WxShareServiceImpl extends ServiceImpl<WxShareMapper, WxShare> implements IWxShareService {

	@Override
	public WxShareVo queryById(Long id){
		return getVoById(id, WxShareVo.class);
	}

	@Override
	public TableDataInfo<WxShareVo> queryPageList(WxShareQueryBo bo) {
		PagePlus<WxShare, WxShareVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), WxShareVo.class);
		return PageUtils.buildDataInfo(result);
	}

	@Override
	public List<WxShareVo> queryList(WxShareQueryBo bo) {
		return listVo(buildQueryWrapper(bo), WxShareVo.class);
	}

	private LambdaQueryWrapper<WxShare> buildQueryWrapper(WxShareQueryBo bo) {
		Map<String, Object> params = bo.getParams();
		LambdaQueryWrapper<WxShare> lqw = Wrappers.lambdaQuery();
		lqw.eq(StrUtil.isNotBlank(bo.getGoodsPosterBg()), WxShare::getGoodsPosterBg, bo.getGoodsPosterBg());
		lqw.eq(StrUtil.isNotBlank(bo.getGrouponPosterBg()), WxShare::getGrouponPosterBg, bo.getGrouponPosterBg());
		lqw.eq(StrUtil.isNotBlank(bo.getImage()), WxShare::getImage, bo.getImage());
		lqw.eq(StrUtil.isNotBlank(bo.getTitle()), WxShare::getTitle, bo.getTitle());
		lqw.eq(StrUtil.isNotBlank(bo.getUserPosterBg()), WxShare::getUserPosterBg, bo.getUserPosterBg());
		return lqw;
	}

	@Override
	public Boolean insertByAddBo(WxShareAddBo bo) {
		WxShare add = BeanUtil.toBean(bo, WxShare.class);
		validEntityBeforeSave(add);
		return save(add);
	}

	@Override
	public Boolean updateByEditBo(WxShareEditBo bo) {
		WxShare update = BeanUtil.toBean(bo, WxShare.class);
		validEntityBeforeSave(update);
		return updateById(update);
	}

	/**
	 * 保存前的数据校验
	 *
	 * @param entity 实体类数据
	 */
	private void validEntityBeforeSave(WxShare entity){
		//TODO 做一些数据校验,如唯一约束
	}

	@Override
	public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
		if(isValid){
			//TODO 做一些业务上的校验,判断是否需要校验
		}
		return removeByIds(ids);
	}
}
